package com.oracle.oa.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.DepartInfoBiz;
import com.oracle.oa.biz.FiletypeinfoBiz;
import com.oracle.oa.biz.LoginLogBiz;
import com.oracle.oa.biz.MessageBiz;
import com.oracle.oa.biz.MessageToUserBiz;
import com.oracle.oa.biz.MessageTypeBiz;
import com.oracle.oa.biz.MyNoteBiz;
import com.oracle.oa.biz.OperatelogBiz;
import com.oracle.oa.biz.ReadCommonMessageBiz;
import com.oracle.oa.biz.UserInfoBiz;
import com.oracle.oa.biz.UserstateBiz;
import com.oracle.oa.biz.WorkTimeBiz;
import com.oracle.oa.biz.impl.OperatelogBizImpl;
import com.oracle.oa.entity.Departinfo;
import com.oracle.oa.entity.Filetypeinfo;
import com.oracle.oa.entity.Message;
import com.oracle.oa.entity.Messagetouser;
import com.oracle.oa.entity.Messagetype;
import com.oracle.oa.entity.Mynote;
import com.oracle.oa.entity.Readcommonmessage;
import com.oracle.oa.entity.Roleinfo;
import com.oracle.oa.entity.Userinfo;
import com.oracle.oa.entity.Userstate;
import com.oracle.oa.entity.Worktime;

public class LoginAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;
	Map<String, Object> session;
	private Long userid;
	private String pwd;
	private String url;
	UserInfoBiz ubiz;
	DepartInfoBiz dbiz;
	MessageBiz mbiz;
	MessageToUserBiz mtubiz;
	ReadCommonMessageBiz rcmbiz;
	MessageTypeBiz mtbiz;
	WorkTimeBiz wtbiz;
	LoginLogBiz loginlogbiz;
	OperatelogBiz obiz;
	MyNoteBiz mnbiz;
	FiletypeinfoBiz ftBiz;
	UserstateBiz usbiz;

	@Override
	public String execute() throws Exception {

		String str = ubiz.login(userid,pwd);
		// 根据用户id查找 //修改人 白涛 （在else中需要插入角色信息 所以 改为 成员变量）
		Userinfo u = (Userinfo) ubiz.getById(Userinfo.class, userid);
		if ("ok".equals(str)) {
			// 查找所有部门
			List<Departinfo> departinfoList = dbiz.getAll(Departinfo.class);
			// 查找所有员工
			List<Userinfo> userinfoList = ubiz.getAll(Userinfo.class);
			//获取用户角色
			int Roleid = u.getRoleinfo().getRoleid();   
			
			//查询所有的消息message
			List<Message> messageList=mbiz.getAll(Message.class);

			List<Message> messagelist = null;
			// 查询所有的消息类型messagetype
			List<Messagetype> messagetypeList = mtbiz.getAll(Messagetype.class);
			// 查询用户已读消息表readcommonmessage
			List<Readcommonmessage> readcommonmessageList = rcmbiz
					.getAll(Readcommonmessage.class);
			// 查询消息发送对象表所有内容mesagetouser
			List<Messagetouser> messagetouserList = mtubiz
					.getAll(Messagetouser.class);
			// 查询收件箱中的信息
			List<Message> inboxlist = mbiz.getByProperty(Message.class,
					"userinfo.userid", userid);
			// 查询员工总数
			int userinfocount = ubiz.getAllusercount();
			// 查找便签信息（张冰月）
			/* List<Mynote> mynoteList=mnbiz.getAll(Mynote.class); */
			// 便签信息修改，只能查改个人便签（张冰月/6月8号改）
			List<Mynote> my = mnbiz.getByProperty(Mynote.class,
					"userinfo.userid", userid);

			// 查询部门信息（武明月）
			List<Departinfo> dptlist = dbiz.getAll(Departinfo.class);

			// 查询上下班时间表（武明月）
			List<Worktime> worktimelist = wtbiz.getAll(Worktime.class);
			Worktime worktime = worktimelist.get(0);
			String ontime = worktime.getOndutytime();
			String offtime = worktime.getOffdutytime();

			// 获取系统当前时间（武明月）
			Timestamp now = new Timestamp(System.currentTimeMillis());
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			String day = sf.format(now);

			String last = sf.format(now.getTime() - 86400000);

			// 获取所有的角色信息
			List<Roleinfo> rList = ubiz.getAll(Roleinfo.class);
			// 获取所有的用户状态
			List<Userstate> uslist = usbiz.getAll(Userstate.class);

			String departname = u.getDepartid().getDepartid()+"";//用户所在部门(冰月)
			Departinfo depart=dbiz.findByDepartInfoName(departname); //查找用户所在部门对象
			session.put("depart", depart);  //用户所在部门的名称(冰月)
			session.put("Roleid", Roleid);

			//获取除了文件夹以外的文件类型开始(小牛)
			List<Filetypeinfo> filetypeinfoList=ftBiz.findOther();
			session.put("filetypeinfoList", filetypeinfoList);
			
			session.put("ontime", ontime); // 正常上班时间(武明月)
			session.put("offtime", offtime); // 正常下班时间(武明月)
			session.put("last", last); // (武明月)
			session.put("day", day); // (武明月)
			session.put("now", now); // (武明月)
			session.put("userid", userid); // 用户id(武明月)
			session.put("dptlist", dptlist); // 所有部门信息(武明月)

			session.put("rList", rList);
			session.put("uslist", uslist);
			session.put("my", my); // 查看个人标签
			session.put("messageList", messageList);// 所有消息
			session.put("messagetypeList", messagetypeList);// 消息类型
			session.put("readcommonmessageList", readcommonmessageList);// 用户已读消息
			session.put("messagetouserList", messagetouserList);// 消息发送对象信息
			session.put("inboxlist", inboxlist);// 收件箱信息
			session.put("userinfocount", userinfocount);// 员工总数

			session.put("messageList", messageList);// 所有消息
			session.put("messagelist", messagelist);
			session.put("messagetypeList", messagetypeList);// 消息类型
			session.put("readcommonmessageList", readcommonmessageList);// 用户已读消息
			session.put("messagetouserList", messagetouserList);// 消息发送对象信息
			session.put("inboxlist", inboxlist);// 收件箱信息
			session.put("userinfocount", userinfocount);// 员工总数
			session.put("departinfoList", departinfoList); // 部门信息
			session.put("userinfoList", userinfoList); // 员工信息
			session.put("userinfo", u);

			if (1 == Roleid) {
				url = "admin_cg/main.jsp";
				session.put("url", "admin_cg");
				System.out.println("超级管理员登录成功！");
			}
			if (2 == Roleid) {
				url = "admin_pg/main.jsp";
				session.put("url", "admin_pg");
				System.out.println("普通管理员登录成功！");
			}
			if (3 == Roleid) {
				url = "admin_pt/main.jsp";
				session.put("url", "admin_pt");
				System.out.println("普通用户登录成功！");
			}
			// 登录成功日志写入
			loginlogbiz.add(u, 1);
			OperatelogBizImpl.user = u;
		} else {
			session.put("error", str);
			url = "Login.jsp";
			// 登录失败日志写入
			if(str.equals("密码错误")){
				loginlogbiz.add(u, 2);
			}
		}

		return SUCCESS;
	}

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public UserInfoBiz getUbiz() {
		return ubiz;
	}

	public void setUbiz(UserInfoBiz ubiz) {
		this.ubiz = ubiz;
	}

	public DepartInfoBiz getDbiz() {
		return dbiz;
	}

	public void setDbiz(DepartInfoBiz dbiz) {
		this.dbiz = dbiz;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;

	}

	public MessageBiz getMbiz() {
		return mbiz;
	}

	public void setMbiz(MessageBiz mbiz) {
		this.mbiz = mbiz;
	}

	public MessageToUserBiz getMtubiz() {
		return mtubiz;
	}

	public void setMtubiz(MessageToUserBiz mtubiz) {
		this.mtubiz = mtubiz;
	}

	public ReadCommonMessageBiz getRcmbiz() {
		return rcmbiz;
	}

	public void setRcmbiz(ReadCommonMessageBiz rcmbiz) {
		this.rcmbiz = rcmbiz;
	}

	public MessageTypeBiz getMtbiz() {
		return mtbiz;
	}

	public void setMtbiz(MessageTypeBiz mtbiz) {
		this.mtbiz = mtbiz;
	}

	public WorkTimeBiz getWtbiz() {
		return wtbiz;
	}

	public void setWtbiz(WorkTimeBiz wtbiz) {
		this.wtbiz = wtbiz;
	}

	public LoginLogBiz getLoginlogbiz() {
		return loginlogbiz;
	}

	public void setLoginlogbiz(LoginLogBiz loginlogbiz) {
		this.loginlogbiz = loginlogbiz;
	}

	public OperatelogBiz getObiz() {
		return obiz;
	}

	public void setObiz(OperatelogBiz obiz) {
		this.obiz = obiz;
	}

	public MyNoteBiz getMnbiz() {
		return mnbiz;
	}

	public void setMnbiz(MyNoteBiz mnbiz) {
		this.mnbiz = mnbiz;
	}

	public UserstateBiz getUsbiz() {
		return usbiz;
	}

	public void setUsbiz(UserstateBiz usbiz) {
		this.usbiz = usbiz;
	}

	public FiletypeinfoBiz getFtBiz() {
		return ftBiz;
	}

	public void setFtBiz(FiletypeinfoBiz ftBiz) {
		this.ftBiz = ftBiz;
	}

	
}
